﻿Imports System.Data.SqlClient

Public Class cn_searchproductlist
    Inherits System.Web.UI.Page
    Dim objrd As SqlDataReader = Nothing
    Dim objrdCom As SqlDataReader = Nothing
    Private i As Integer
    Private pages As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim name, country, strErr, comId, comName, countryCode, strProduct As String
        strProduct = ""
        strErr = ""
        Dim objdt = New GetData
        Dim objdtCom = New GetData
        Dim ps = New PreparedStatement("")
        Dim psc = New PreparedStatement("")
        Dim photo_path As String = ""
        Dim sum, sumdesc As Integer
        i = Request("page")
        If i = Nothing Or i = 0 Then
            i = 1
        End If
            If (Request("name") <> Nothing) Then
                name = Request("name")
            Else
                name = ""
            End If
            If (Request("country") <> Nothing) Then
                country = Request("country")
            Else
                country = ""
            End If
            ps.strSql = "select count(*) from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                     " where pi.Product_name like '%" & name & "%' and pi.lang_flag='zh' and mi.country like '%" & country & "%'"
        ps.parameters.Clear()
        Dim conn As SqlConnection = objdt.getConn()

        objrd = objdt.dr(ps, strErr, conn, objrd)
        objrd.Read()
        sum = objrd.Item(0)
        pages = (sum \ 10) + 1
        If i >= pages Then
            i = pages
        ElseIf i <= 0 Then
            i = 1
        End If
        If (i = pages) Then
            sumdesc = sum
        Else
            sumdesc = i * 10
        End If
        totalPage.InnerText = "页" & i.ToString & "/" & pages.ToString()
        totalPage1.InnerText = "页" & i.ToString & "/" & pages.ToString()
        resultNum.InnerText = sum.ToString & "结果"
        ps.strSql = "select top(" & i & "*10) * from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                 " where pi.Product_name like '%" & name & "%' and pi.lang_flag='zh' and mi.country like '%" & country & "%'" & _
                " and pi.product_id not in (" & _
                "select top (" & i - 1 & "*10) product_id from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                 " where pi.Product_name like '%" & name & "%' and pi.lang_flag='zh' and mi.country like '%" & country & "%'" & _
                " order by pi.product_name" & _
                ") and pi.product_id not in (select top(" & sum - sumdesc & ") product_id from product_info pi left join merchant_info mi on pi.company_id = mi.company_id" & _
                 " where pi.Product_name like '%" & name & "%' and pi.lang_flag='zh' and mi.country like '%" & country & "%' " & _
                "order by pi.product_name desc) order by pi.product_name"
        objrd = objdt.dr(ps, strErr, conn, objrd)
        If objrd.HasRows Then
            Dim con As SqlConnection = objdtCom.getConn()
            
            While (objrd.Read())
                If (objrd.Item("photo_path").ToString = Nothing) Then
                    photo_path = "/pic/nopic180.gif"
                Else
                    photo_path = objrd.Item("photo_path").ToString
                End If
                comId = objrd.Item("company_id")
                psc.strSql = "select * from merchant_info where company_id = @comId and lang_flag='zh' order by company_name"
                psc.addParameter("comId", comId)

                objrdCom = objdtCom.dr(psc, strErr, con, objrdCom)
                If objrdCom.HasRows Then
                    objrdCom.Read()
                    comName = objrdCom.Item("company_name").ToString
                    country = objrdCom.Item("country").ToString
                    If (country.ToLower.Equals("china")) Then
                        countryCode = "../assets/cn.gif"
                        country = "中国"
                    ElseIf (country.ToLower.Equals("usa")) Then
                        countryCode = "../assets/us.gif"
                        country = "美国"
                    End If
                End If
                strProduct &= "<div class=""itemBox"">" & _
                                    "<div class=""box2"">" & _
                                        "<div class=""images"">" & _
                                            "<a target=""_blank"" dot=""area=ml2&type;=pimg&value;="" value=""#"">" & _
                                                "<img  border=""0"" isoem=""true"" alt=""" & objrd.Item("product_name") & """ src=""" & photo_path & """ width=""100px"" height=""75px""/>" & _
                                            "</a>" & _
                                        "</div>" & _
                                    "</div>" & _
                                    "<div class=""box5 products"">" & _
                                            "<div style=""margin-bottom: 5px;"">" & _
                                                "供货商:" & _
                                                "<a class=""company"" target=""_blank"" dot=""area=ml2&type;=pttl&value;="" href=""cn_company.aspx?cId=" & comId & """>" & comName & "</a>" & _
                                            "</div>" & _
                                            "<div class=""location"">" & _
                                                "<img style=""border: 0pt none; vertical-align: top;"" alt=""country flag"" src=""" & countryCode & """/>" & _
                                                country & _
                                            "</div>" & _
                                    "</div>" & _
                                    "<div class=""box3"">" & _
                                            "<div class=""productname"">" & _
                                                "<a  target=""_blank"" title="""" dot=""area=ml2&type;=pttl&value;="" href=""cn_productdetail.aspx?productId=" & objrd.Item("product_id") & """>" & _
                                                objrd.Item("product_name") & _
                                                "</a>" & _
                                            "</div>" & _
                                            "<div class=""description"">" & _
                                                objrd.Item("description") & _
                                                "<br>" & _
                                            "</div>" & _
                                    "</div>" & _
                            "</div>"
            End While
            objdtCom.closeConn(conn)
        End If
        objdt.closeConn(conn)

            Me.country.Value = Request("country")
            Me.name.Value = Request("name")
            prePage.HRef = "cn_serchproductlist.aspx?country=" & Request("country") & "&name=" & name & "&page=" & i - 1
            nextPage.HRef = "cn_searchproductlist.aspx?country=" & Request("country") & "&name=" & name & "&page=" & i + 1
            productList.InnerHtml = strProduct

    End Sub

    Protected Sub go_Click(ByVal sender As Object, ByVal e As EventArgs) Handles go.Click
        Response.Redirect("cn_searchproductlist.aspx?country=" & country.Value & "&name=" & name.Value & "&page=" & pageNum.Text)
    End Sub
End Class